package acl.bussiness;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import acl.hibernate.dao.Departmentdao;
import acl.hibernate.po.OaDepartments;



public class DepartmentBussiness {
	
	String xml = "<?xml version='1.0' encoding='GBK'?><tree id='0'>";
	Departmentdao department = new Departmentdao();	
	private String children(int now, List<Object> list) {
		/*
		OaDepartments children = (OaDepartments) list.get(now);
		int selfId = children.getId();
		list = department.treeBypid(selfId);
		if (list.size() == 0) {
			xml = xml + "<item text='" + children.getYxsmc() + "' id='"
					+ children.getId() + "'></item>";
		} else {
			xml = xml + "<item text='" + children.getYxsmc() + "' id='"
					+ children.getId() + "'>";
			list = department.treeBypid(children.getId());
			for (now = 0; now < list.size(); now++) {
				this.children(now, list);
			}
			xml = xml + "</item>";
		}
		
		return xml;
		*/
		return "";
	}
	public String treeXml(Integer id) {
		
		List self = department.tree(id);
		if (self.size() == 0) {
			xml += "<item text='无部门' id='no'>";
		} else {
			xml += "<item text='" + ((OaDepartments) self.get(0)).getYxsmc()
					+ "' id='" + ((OaDepartments) self.get(0)).getId() + "'>";
			int pid = ((OaDepartments) self.get(0)).getId();
			List<Object> list = department.treeBypid(pid);
			if (list.size() == 0) {
				xml = xml + "</item></tree>";
				return xml;
			} else {
				for (int now = 0; now < list.size(); now++) {
					xml = this.children(now, list);
				}
			}
		}
		return xml + "</item></tree>";		
		
	}

	// --------------------------------------------------------------------------------------------
	public void creatNewDepartment(Integer pid, String name, String number,
			Integer userId) throws Exception {
		// 根据父节点id和新节点name，用户id,编号创建节点
		try {
			Date day = new Date();
			DateFormat dayformat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			String day1 = dayformat.format(day);
			day = dayformat.parse(day1);
			OaDepartments department = new OaDepartments();
			department.setDepartmentState((new Integer(1)).byteValue());
			department.setParentId(pid);// 父节点
			department.setCreated(day);// 创建日期
			department.setModified(day);// 修改日期
			department.setCreateuserId(userId);// 创建人id
			Byte s = 1;
			department.setDepartmentState(s);// 节点状态
			department.setYxsh(number);// 节点编号
			department.setYxsmc(name);// 节点名
			Byte c = 0;
			department.setChild(c);
			Departmentdao dd = new Departmentdao();
			dd.creatNewDepartment(department);
		} catch (Exception e) {
			throw e;
		}
	}
	//--------------------------------------------------------
	public List getAllDepartment() {
		// 获取所有部门
		return department.getValidDepartment();		
	}
	public void deleteDepartment(Integer pid){
		// 根据部门id，删除当前部门及所有子部门部门
		// Author:Silence
		department.deleteDepartments(pid);
	}
	public void reNameDepartment(Integer id, String name,String number2){
		// 根据部门id及其新名对部门进行重命名
		// Author:Silence
		department.reNameDepartment(id, name,number2);
	}
	public void reWriteContent(Integer id,String content){
		// 根据部门id及其简介修改部门介绍
		// Author:Silence
		department.reWriteContent(id, content);
	}
	//以下部分由熊凯添加
	//2009-7-4
	/*
	 * 批量删除部门
	 */
	public boolean deleteDepartment(String[] idArray)
	{
		return department.deleteDepartment(idArray); 
	}
	/*
	 * 根据id获取部门对象
	 */
	public OaDepartments getDepartment(Integer departmentId)
	{
		return (OaDepartments) department.getById(OaDepartments.class.getName(), departmentId);
	}
	public boolean saveDepartment(OaDepartments adepartment)
	{		
		return department.saveDepartment(adepartment);
	}
	
}